package com.ly.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ly.pojo.Article;
import com.ly.pojo.PageBean;
import com.ly.pojo.Result;
import com.ly.service.ArticleServeice;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleServeice articleServeice;
   
    @PostMapping
    public Result add(@RequestBody @Validated(Article.Add.class) Article article){
        articleServeice.add(article);
        return Result.success();
    }
    
    @GetMapping
    public Result<PageBean<Article>> list(Integer pageNum,Integer pageSize,@RequestParam(required=false) Integer categoryId,@RequestParam(required=false) String state){
        PageBean<Article> pb =  articleServeice.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }
    
    @PutMapping
    public Result update(@RequestBody @Validated(Article.Update.class)  Article article){
        articleServeice.update(article);
        return Result.success();
    }

    @GetMapping("/detail")
    public Result<Article> detail(Integer id){
        Article detail = articleServeice.findById(id);
        return Result.success(detail);
    }

    @DeleteMapping
    public Result deleted(Integer id){
        articleServeice.delete(id);
        return Result.success();
    }
}
